<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>demo</title>
</head>

<body>
<div id="app" style="width:100%">
    <div style="margin-left: 1vw;margin-right: 1vw;margin-top:1vw;">
        <template>
            <el-button @click="test_get" type="primary">测试get</el-button>
            <el-button @click="test_post" type="primary">测试post</el-button>
        </template>
    </div>
</div>

<link rel="stylesheet" type="text/css" href="/static/css/element-ui.css">

<script src="/static/js/vue.js" type="text/javascript"></script>
<script src="/static/js/element-ui.js"></script>
<script src="/static/js/axios.js" type="text/javascript"></script>
<script type="text/javascript" src="/static/js/socket.io.min.4.3.1.js"></script>

<script>
    let app = new Vue({
        el: '#app',
        name: 'user',
        filters: {},
        data() {
            return {
                socket: {
                    message_url: 'http://' + document.domain + ':' + location.port + '/message',
                    data: null,
                },
                test: "test",
                headers: {
                    token: '{[ token ]}',
                },
            }
        },
        created() {
            this.socket_connect()
            this.socket.data.on('message', function (data) {
                console.log('收到消息：%o', data);
            });

            this.socket.data.on('disconnect', this.socket_connect);
        },
        methods: {
            socket_connect() {
                this.socket.data = io.connect(this.socket.message_url);
            },
            test_get() {
                axios.get('/test_get', {params: {test: this.test}, headers: this.headers}).then(res => {
                    this.$message({
                        message: '测试成功！status=' + res.data.status,
                        type: 'success'
                    });
                })
            }, test_post() {
                axios.post('/test_post', {test: this.test}, {headers: this.headers}).then(res => {
                    this.$message({
                        message: '测试成功！status=' + res.data.status,
                        type: 'success'
                    });
                })
            }
        }
    })
</script>
</body>
</html>
